#import <UIKit/UIKit.h>
#import "DrawTools.h"
#import "Tools.h"

#define IPHONE4_OR_LESS (MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) < 568)
#define IPHONE5_OR_LATER (MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) >= 568)
#define IPHONE5_568 (MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) == 568)
#define IPHONE6_LATER (MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) >= 667)
#define IPHONE6P (MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) == 736)

#define K_IPHONE4_Font              9.0
#define K_IPHONE5_Font              10.0
#define K_IPHONE6_Font              11.0
#define K_Font                (IPHONE6_LATER? K_IPHONE6_Font:(IPHONE5_568? K_IPHONE5_Font:K_IPHONE4_Font))

#define RECT_WIDTH(r) (r.nRight-r.nLeft)
#define RECT_HEIGHT(r) (r.nBottom-r.nTop)

void DrawTools_FullRect(void *g, GUI_RECT *pRect, int nColor)
{
	CGRect rect ;
	CGContextRef context;
	context = (CGContextRef)g;
	rect = CGRectMake(pRect->nLeft, pRect->nTop, (pRect->nRight - pRect->nLeft) , (pRect->nBottom - pRect->nTop) );
	
	if(context == nil) return;
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
	CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);

	CGContextFillRect(context, rect);

}

void DrawTools_FullARect(void *g, GUI_RECT *pRect, int nColor, float a)
{
    CGRect rect ;
	CGContextRef context;
	context = (CGContextRef)g;
	rect = CGRectMake(pRect->nLeft, pRect->nTop, (pRect->nRight - pRect->nLeft) , (pRect->nBottom - pRect->nTop) );
	
	if(context == nil) return;
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
	CGContextSetRGBFillColor(context, rr, gg, bb, a);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, a);
    
	CGContextFillRect(context, rect);
}

void DrawTools_FullRectArr(void *g, GUI_RECT *pRect, int nLen, int nColor)
{
	CGRect rect ;
	CGContextRef context;
	context = (CGContextRef)g;
	
	
	if(context == nil) return;
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
	CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);
	
	for(int i=0; i<nLen; i++)
	{
		rect = CGRectMake(pRect[i].nLeft, pRect[i].nTop, RECT_WIDTH(pRect[i]) , RECT_HEIGHT(pRect[i]));
		CGContextFillRect(context, rect);
	}
}

void DrawTools_Full_RoundRect(void* g, GUI_RECT* pRect, int nColor, int cornerX, int cornerY)
{
	
	CGRect rect ;
	CGContextRef context;
	context = (CGContextRef)g;
	rect = CGRectMake(pRect->nLeft, pRect->nTop, (pRect->nRight - pRect->nLeft) , (pRect->nBottom - pRect->nTop) );
	
	if(context == nil) return;
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
	CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);
	
	
	CGContextSaveGState(context); // 2
    CGContextTranslateCTM (context, CGRectGetMinX(rect), // 3
                           CGRectGetMinY(rect));
    CGContextScaleCTM (context, cornerX, cornerY); // 4
	
    float fw = CGRectGetWidth (rect) / cornerX; // 5
    float fh = CGRectGetHeight (rect) / cornerY; // 6
    CGContextMoveToPoint(context, fw, fh/2); // 7
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // 8
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // 9
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // 10
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // 11
    CGContextClosePath(context); // 12
    CGContextRestoreGState(context); // 13
	
	CGContextFillPath(context);
}

void DrawTools_Draw_RoundRect(void* g, GUI_RECT* pRect, int nColor, int penStyle, int cornerX, int cornerY)
{
	
	CGRect rect ;
	CGContextRef context;
	context = (CGContextRef)g;
	rect = CGRectMake(pRect->nLeft, pRect->nTop, (pRect->nRight - pRect->nLeft) , (pRect->nBottom - pRect->nTop) );
	
	if(context == nil) return;
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
	CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);
	
	
	CGContextSaveGState(context); // 2
    CGContextTranslateCTM (context, CGRectGetMinX(rect), // 3
                           CGRectGetMinY(rect));
    CGContextScaleCTM (context, cornerX, cornerY); // 4
	
    float fw = CGRectGetWidth (rect) / cornerX; // 5
    float fh = CGRectGetHeight (rect) / cornerY; // 6
    CGContextMoveToPoint(context, fw, fh/2); // 7
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // 8
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // 9
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // 10
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // 11
    CGContextClosePath(context); // 12
    CGContextRestoreGState(context); // 13
	
	//CGContextSetFillColor(context, gray);
    CGContextStrokePath(context);
}

void DrawTools_DrawRect(void *g, GUI_RECT *pRect, int nColor, int penStyle)
{
	CGRect rect ;
	CGContextRef context;
	context = (CGContextRef)g;
	rect = CGRectMake(pRect->nLeft, pRect->nTop, (pRect->nRight - pRect->nLeft) , (pRect->nBottom - pRect->nTop) );
	
	if(context == nil) return;
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
    
    CGContextSetLineWidth(context, 0.5);
  
  CGContextSaveGState((CGContextRef)g);
  CGContextSetShouldAntialias((CGContextRef)g, NO);
	CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);
	CGContextAddRect(context, rect);
	CGContextStrokePath(context);
  CGContextRestoreGState((CGContextRef)g);
}

void DrawTools_DrawRectArr(void *g, GUI_RECT *pRect, int nLen, int nColor, int penStyle)
{
	CGRect rect ;
	CGContextRef context;
	context = (CGContextRef)g;
	
	
	if(context == nil) return;
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
  CGContextSaveGState((CGContextRef)g);
  CGContextSetShouldAntialias((CGContextRef)g, NO);
	CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);
	for(int i=0; i<nLen; i++)
	{
		rect = CGRectMake(pRect[i].nLeft, pRect[i].nTop, RECT_WIDTH(pRect[i]) , RECT_HEIGHT(pRect[i]));
		CGContextAddRect(context, rect);
	}
	CGContextStrokePath(context);
  CGContextRestoreGState((CGContextRef)g);
}

void DrawTools_DrawRectArrForKChartUp(void *g, GUI_RECT *pRect, int nLen, int nColor, int nColor2, int penStyle)
{
    int w;
    w = RECT_WIDTH(pRect[0]);
    if(w ==1)
    {
        DrawTools_FullRectArr(g, pRect, nLen, nColor);
        return;
    }
  
  CGContextSaveGState((CGContextRef)g);
  CGContextSetShouldAntialias((CGContextRef)g, NO);
	DrawTools_FullRectArr(g, pRect, nLen, nColor2);
  DrawTools_DrawRectArr(g, pRect, nLen, nColor, penStyle);
  CGContextRestoreGState((CGContextRef)g);
}

void DrawTools_DrawText(void *g, GUI_RECT *pRect, char *pText, int nColor, int nAgin)
{
	DrawTools_DrawFText(g, pRect, pText, nColor, nAgin, 10.0);
}

void DrawTools_DrawFText(void *g, GUI_RECT *pRect, char *pText, int nColor, int nAgin, float fSize)
{
	DrawTools_DrawFAText(g, pRect, pText, nColor, nAgin, fSize, 1.0);
}

void DrawTools_DrawFAText(void *g, GUI_RECT *pRect, char *pText, int nColor, int nAgin, float fSize, float a)
{
    CGRect rect ;
	CGContextRef context;
	//int nFontHeight;
	context = (CGContextRef)g;
	rect = CGRectMake(pRect->nLeft, pRect->nTop, (pRect->nRight - pRect->nLeft) , (pRect->nBottom - pRect->nTop) );
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
	CGContextSetRGBFillColor(context, rr, gg, bb, a);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, a);
	
	
	//CGContextSetFontSize(context, 12);
	//char *ppu;
	NSString *unicode = [NSString stringWithUTF8String:pText];
	
	int x,y,r,b;
	y = pRect->nTop;
	b = pRect->nBottom;
	r = pRect->nRight;
	x = pRect->nLeft;
	UITextAlignment iAlign;
	switch (nAgin) {
		case AGIN_LEFT:
			iAlign = UITextAlignmentLeft;
			break;
		case AGIN_CENTER:
			iAlign = UITextAlignmentCenter;
			break;
		case AGIN_RIGHT:
			iAlign = UITextAlignmentRight;
			break;
		default:
			x = pRect->nLeft;
			break;
	}
	UIFont *pFont = [UIFont systemFontOfSize:fSize];
//	rect.origin.y = rect.origin.y + (rect.size.height - pFont.pointSize)/2;
//	rect.size.height = pFont.pointSize;
  rect.origin.y = rect.origin.y + (rect.size.height - pFont.pointSize + pFont.descender)/2;
	rect.size.height = pFont.pointSize;
	[unicode drawInRect:rect withFont:pFont lineBreakMode:UILineBreakModeClip alignment:iAlign];
}

void DrawTools_DrawNSText(void *g, GUI_RECT *pRect, void *pText, int nColor, int nAgin)
{
	DrawTools_DrawNSFText(g, pRect, pText, nColor, nAgin, K_Font);
}

void DrawTools_DrawNSFText(void *g, GUI_RECT *pRect, void *pText, int nColor, int nAgin, float fSize)
{
	CGRect rect ;
	CGContextRef context;
	//int nFontHeight;
	context = (CGContextRef)g;
	rect = CGRectMake(pRect->nLeft, pRect->nTop, (pRect->nRight - pRect->nLeft) , (pRect->nBottom - pRect->nTop) );
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
	CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);
	
	
	//CGContextSetFontSize(context, fSize);
	
	NSString *unicode = (NSString*)pText;
	
//	int x,y,r,b;
//	y = pRect->nTop;
//	b = pRect->nBottom;
//	r = pRect->nRight;
//	x = pRect->nLeft;
	UITextAlignment iAlign;
	switch (nAgin) {
		case AGIN_LEFT:
			iAlign = NSTextAlignmentLeft;
			break;
		case AGIN_CENTER:
			iAlign = NSTextAlignmentNatural;
			break;
		case AGIN_RIGHT:
			iAlign = NSTextAlignmentRight;
			break;
//		default:
//			x = pRect->nLeft;
//			break;
	}
	UIFont *pFont = [UIFont systemFontOfSize:fSize];
	rect.origin.y = rect.origin.y + (rect.size.height - pFont.pointSize + pFont.descender)/2;
	rect.size.height = pFont.pointSize;
  //rect = CGRectMake(pRect->nLeft, pRect->nTop+(rect.size.height - pFont.pointSize)/2, (pRect->nRight - pRect->nLeft) , pFont.pointSize );
	[unicode drawInRect:rect withFont:pFont lineBreakMode:NSLineBreakByClipping alignment:iAlign];
  
//  [unicode drawAtPoint:rect.origin forWidth:rect.size.width withFont:pFont fontSize:fSize lineBreakMode:UILineBreakModeClip baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
}

void DrawTools_DrawLine(void *g, int x1, int y1, int x2, int y2, int nColor,int penStyle)
{
    DrawTools_DrawNewLine(g, x1, y1, x2, y2, nColor, penStyle, 0.5);
}

void DrawTools_DrawNewLine(void *g, int x1, int y1, int x2, int y2, int nColor,int nStyle, float fLineW)
{
    CGContextRef context;
    context = (CGContextRef)g;
    
    if(context == nil) return;
    
    CGFloat rr,gg,bb;
    rr = ((nColor>>16)&0xff)/255.0;
    gg = ((nColor>>8)&0xff)/255.0;
    bb = (nColor&0xff)/255.0;
    
    CGContextSetLineWidth(context, fLineW);
    
    CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
    CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);
    
    CGContextSetAllowsAntialiasing(context, NO);
    
    if (nStyle!=0) {
        CGFloat dash1[] = {1, 1};//虚线长度每一根
        CGContextSetLineDash(context, 0, dash1, 1);
    }
    CGContextMoveToPoint(context, x1+0.0, y1+0.0);
    CGContextAddLineToPoint(context, x2+0.0, y2+0.0);
    
    CGContextStrokePath(context);
    CGContextSetLineDash(context, 0, 0, 0);
    
    CGContextSetAllowsAntialiasing(context, YES);
}

void DrawTools_DrawPath (void * g, GUI_POINT  *points, int nLen, int nColor)
{
	CGContextRef context;
	context = (CGContextRef)g;
	
	if(context == nil) return;
	if(points==NULL)
	{
		return;
	}
	
	GUI_POINT  *p;
	p = points;
	
	CGFloat rr,gg,bb;
	rr = ((nColor>>16)&0xff)/255.0;
	gg = ((nColor>>8)&0xff)/255.0;
	bb = (nColor&0xff)/255.0;
	CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
	CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);

	if(p->y<0 || nLen<=0)
	{
		return;
	}
	
	if (p->y>0) {
		CGContextMoveToPoint(context, p->x, p->y);
		for(int i=1; i<nLen; i++)
		{
			p = p+1;
			CGContextAddLineToPoint(context, p->x, p->y);
		}
	}
	else 
	{
		int i;
		for(i=1; i<nLen; i++)
		{
			if (p->y>0) {
				break;
			}
			p = p+1;
		}
		
		CGContextMoveToPoint(context, p->x, p->y);
		for(; i<nLen; i++)
		{
			p = p+1;
			CGContextAddLineToPoint(context, p->x, p->y);
		}
	}

	
	
	CGContextStrokePath(context);

}

CGPoint demoLGStart(CGRect bounds)
{
	return CGPointMake(bounds.origin.x, bounds.origin.y + bounds.size.height * .001);
}

CGPoint demoLGEnd(CGRect bounds)
{
	return CGPointMake(bounds.origin.x, bounds.origin.y + bounds.size.height * .999);
}

CGGradientDrawingOptions drawingOptions()
{
	CGGradientDrawingOptions options = 0;
	if(NO)
	{
		options |= kCGGradientDrawsBeforeStartLocation;
	}
	if(YES)
	{
		options |= kCGGradientDrawsAfterEndLocation;
	}
	return options;
}

void DrawTools_DrawBezierPath (void * g, GUI_POINT  *points, int nLen, int y, int nSColor, int nEColor)
{
	CGContextRef context;
	context = (CGContextRef)g;
	
	if(context == nil) return;
	if(points==NULL)
	{
		return;
	}
	
	GUI_POINT  *p;
	p = points;
	
	if(p->y<0 || nLen<=0)
	{
		return;
		
	}
	
	CGContextSaveGState(context);
	
	
	CGContextMoveToPoint(context, p->x, p->y);
	for (int i = 1; i < nLen; i++) {
		p = p+1;
		CGContextAddLineToPoint(context, p->x, p->y);
	}
	CGContextAddLineToPoint(context, p->x, y);
	CGContextAddLineToPoint(context, points->x, y);
	
	CGContextClosePath(context);
	
	CGContextClosePath(context);
	CGContextClip(context);
	
	
	CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
	CGFloat colors[] =
	{
		((nSColor>>16)&0xff) / 255.0, ((nSColor>>8)&0xff) / 255.0, ((nSColor)&0xff) / 255.0, 1.0,//起始
		((nEColor>>16)&0xff) / 226.0, ((nEColor>>8)&0xff) / 255.0, ((nEColor)&0xff) / 255.0, 0.0,//结束
	};
//	CGFloat colors[] =
//	{
//		209.0 / 255.0, 54.0 / 255.0, 0.0 / 255.0, 0.8,//起始
//		239.0 / 226.0, 224.0 / 255.0, 221.0 / 255.0, 0.1,//结束
//	};
	CGGradientRef _gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
	CGColorSpaceRelease(rgb);
	
	CGRect clip = CGRectInset(CGContextGetClipBoundingBox(context), 0.0, 0.0);
	CGPoint start, end;
	start = demoLGStart(clip);
	end = demoLGEnd(clip);
	CGGradientDrawingOptions options = drawingOptions();
	CGContextDrawLinearGradient(context, _gradient, start, end, options);
	
	
	
	CGGradientRelease(_gradient);
	CGContextRestoreGState(context);
}

void DrawTools_DrawNSFTextEx(void *g, GUI_RECT *pRect, void *pText, int nColor, int nAgin, float fSize)
{
  CGRect rect ;
  CGContextRef context;
  //int nFontHeight;
  context = (CGContextRef)g;
  rect = CGRectMake(pRect->nLeft, pRect->nTop, (pRect->nRight - pRect->nLeft) , (pRect->nBottom - pRect->nTop) );
  
  CGFloat rr,gg,bb;
  rr = ((nColor>>16)&0xff)/255.0;
  gg = ((nColor>>8)&0xff)/255.0;
  bb = (nColor&0xff)/255.0;
  CGContextSetRGBFillColor(context, rr, gg, bb, 1.0);
  CGContextSetRGBStrokeColor(context, rr, gg, bb, 1.0);
  
  
  //CGContextSetFontSize(context, fSize);
  
  NSString *unicode = (NSString*)pText;
  
  int x,y,r,b;
  y = pRect->nTop;
  b = pRect->nBottom;
  r = pRect->nRight;
  x = pRect->nLeft;
  UITextAlignment iAlign;
  switch (nAgin) {
    case AGIN_LEFT:
      iAlign = UITextAlignmentLeft;
      break;
    case AGIN_CENTER:
      iAlign = UITextAlignmentCenter;
      break;
    case AGIN_RIGHT:
      iAlign = UITextAlignmentRight;
      break;
    default:
      x = pRect->nLeft;
      break;
  }
  UIFont *pFont = [UIFont systemFontOfSize:fSize];
  rect.origin.y = rect.origin.y + (rect.size.height - pFont.pointSize)/2;
  rect.size.height = pFont.pointSize;
  [unicode drawInRect:rect withFont:[UIFont systemFontOfSize:fSize] lineBreakMode:UILineBreakModeTailTruncation alignment:iAlign];
}
